﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using System.Web;

namespace DAL
{
    public class QXHelper
    {
        #region Layui数据表格Json格式
        /// <summary>
        /// Layui数据表格Json格式
        /// </summary>
        /// <param name="Dt">DataTable</param>
        /// <returns>Layui数据表格Json字符串</returns>
        public string LayuiTableJson(DataTable Dt)
        {
            string[] StrDc = new string[Dt.Columns.Count];
            string HeadStr = string.Empty;
            string count = Dt.Rows.Count.ToString();

            if (count == "0")
            {
                return "{\"code\":0,\"msg\": \"\",\"count\":" + count + ",\"data\":[]}";
            }

            else
            {

                for (int i = 0; i < Dt.Columns.Count; i++)
                {
                    StrDc[i] = Dt.Columns[i].Caption;
                    HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
                }

                HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
                StringBuilder Sb = new StringBuilder();
                Sb.Append("{\"code\":0,\"msg\": \"\",\"count\":" + count + ",\"data\":[");

                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    string TempStr = HeadStr;
                    Sb.Append("{");

                    for (int j = 0; j < Dt.Columns.Count; j++)
                    {
                        TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
                    }
                    Sb.Append(TempStr + "},");
                }
                Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
                Sb.Append("]}");

                return Sb.ToString();
            }
        }
        #endregion

        #region Layui带分页数据表格的Json格式
        /// <summary>
        /// Layui带分页数据表格的Json格式
        /// </summary>
        /// <param name="Ds">DataSet</param>
        /// <returns>Layui带分页数据表格Json字符串</returns>
        public string LayuiTablePageJson(DataSet Ds)
        {
            DataTable Dt = new DataTable();
            Dt = Ds.Tables[0];
            string[] StrDc = new string[Dt.Columns.Count];
            string HeadStr = string.Empty;
            string count = Ds.Tables[1].Rows[0][0].ToString();
            if (count == "0")
            {
                return "{\"code\":0,\"msg\": \"\",\"count\":" + count + ",\"data\":[]}";
            }

            else
            {

                for (int i = 0; i < Dt.Columns.Count; i++)
                {
                    StrDc[i] = Dt.Columns[i].Caption;
                    HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
                }

                HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
                StringBuilder Sb = new StringBuilder();
                Sb.Append("{\"code\":0,\"msg\": \"\",\"count\":" + count + ",\"data\":[");

                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    string TempStr = HeadStr;
                    Sb.Append("{");

                    for (int j = 0; j < Dt.Columns.Count; j++)
                    {
                        TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
                    }
                    Sb.Append(TempStr + "},");
                }
                Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
                Sb.Append("]}");

                return Sb.ToString();
            }
        }
        #endregion

        #region TableToJsonOneRows
        public string TableToJsonOneRows(DataTable Dt)
        {
            string[] StrDc = new string[Dt.Columns.Count];
            string HeadStr = string.Empty;
            string count = Dt.Rows.Count.ToString();

            if (count == "0")
            {
                return "{\"count\":0}";
            }

            else
            {

                for (int i = 0; i < Dt.Columns.Count; i++)
                {
                    StrDc[i] = Dt.Columns[i].Caption;
                    HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
                }

                HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
                StringBuilder Sb = new StringBuilder();

                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    string TempStr = HeadStr;
                    Sb.Append("{");

                    for (int j = 0; j < Dt.Columns.Count; j++)
                    {
                        TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
                    }
                    Sb.Append(TempStr + "},");
                }
                Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));


                return Sb.ToString();
            }
        }
        #endregion

        #region TableToJson
        public string TableToJson(DataTable Dt)
        {
            string[] StrDc = new string[Dt.Columns.Count];
            string HeadStr = string.Empty;
            string count = Dt.Rows.Count.ToString();

            if (count == "0")
            {
                return "{\"error\":0}";
            }
            else
            {
                for (int i = 0; i < Dt.Columns.Count; i++)
                {
                    StrDc[i] = Dt.Columns[i].Caption;
                    HeadStr += "\"" + StrDc[i] + "\" : \"" + StrDc[i] + i.ToString() + "¾" + "\",";
                }

                HeadStr = HeadStr.Substring(0, HeadStr.Length - 1);
                StringBuilder Sb = new StringBuilder();
                Sb.Append("[");

                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    string TempStr = HeadStr;
                    Sb.Append("{");

                    for (int j = 0; j < Dt.Columns.Count; j++)
                    {
                        TempStr = TempStr.Replace(Dt.Columns[j] + j.ToString() + "¾", Dt.Rows[i][j].ToString());
                    }
                    Sb.Append(TempStr + "},");
                }
                Sb = new StringBuilder(Sb.ToString().Substring(0, Sb.ToString().Length - 1));
                Sb.Append("]");

                return Sb.ToString();
            }
        }
        #endregion

        #region 文件大小值转换
        public string GetFileSize(long size)
        {
            var num = 1024.00; //byte

            if (size < num)
                return size + "B";
            if (size < Math.Pow(num, 2))
                return (size / num).ToString("f2") + "K"; //kb
            if (size < Math.Pow(num, 3))
                return (size / Math.Pow(num, 2)).ToString("f2") + "M"; //M
            if (size < Math.Pow(num, 4))
                return (size / Math.Pow(num, 3)).ToString("f2") + "G"; //G

            return (size / Math.Pow(num, 4)).ToString("f2") + "T"; //T
        }
        #endregion

        #region 获取客户机IP地址
        public string GetIP()
        {
            string requestClientIpAddress = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(requestClientIpAddress))
                requestClientIpAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            if (string.IsNullOrEmpty(requestClientIpAddress))
                requestClientIpAddress = HttpContext.Current.Request.UserHostAddress;
            return requestClientIpAddress;
        }
        #endregion
    }
}
